package com.haoshui.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.haoshui.domain.ResponseResult;
import com.haoshui.domain.entity.User;
import com.haoshui.domain.vo.UserInfoVo;
import com.haoshui.enums.AppHttpCodeEnum;
import com.haoshui.exception.SystemException;
import com.haoshui.mapper.UserMapper;
import com.haoshui.service.UserService;
import com.haoshui.utils.BeanCopyUitls;
import com.haoshui.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;

@Service("UserService")
public class UserSeviceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    PasswordEncoder passwordEncoder;

    @Override
    public ResponseResult getUserInfo() {
        //获取用户Id
        Long id = SecurityUtils.getUserId();

        //根据Id查找用户信息
        User user = getById(id);
        UserInfoVo userInfoVo = BeanCopyUitls.copyBean(user, UserInfoVo.class);

        //返回
        return ResponseResult.okResult(userInfoVo);
    }

    @Override
    public ResponseResult updateUserInfo(User user) {
        //TODo 只更改部分信息操作
        updateById(user);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult register(User user) {
        //TODO 繁琐代码可以用注解技术解决
        //判断用户信息不能为空
        if(userNameExist(user.getUserName())){
            throw new SystemException(AppHttpCodeEnum.USERNAME_EXIST);
        }
        if(nickNameExist(user.getNickName())){
            throw new SystemException(AppHttpCodeEnum.NICKNAME_EXIST);
        }
        if(emailExist(user.getEmail())){
            throw new SystemException(AppHttpCodeEnum.EMAIL_EXIST);
        }

        //密码加密
        String encode = passwordEncoder.encode(user.getPassword());
        user.setPassword(encode);

        //存入数据库
        save(user);

        return ResponseResult.okResult();
    }

    @Override
    public Page<User> PageUserByCondition(Integer pageNum, Integer pageSize, String userName, String phonenumber, String status) {
        //设置页查询条件
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Objects.nonNull(userName) && StringUtils.hasText(userName), User::getUserName, userName);
        queryWrapper.eq(Objects.nonNull(phonenumber) && StringUtils.hasText(phonenumber),User::getPhonenumber, phonenumber);
        queryWrapper.eq(Objects.nonNull(status) && StringUtils.hasText(status), User::getStatus, status);

        //设置页
        Page<User> page = new Page<>(pageNum, pageSize);

        //查询
        page(page, queryWrapper);

        return page;
    }

    @Override
    public boolean hasName(String userName) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUserName, userName);
        List<User> list = list(queryWrapper);
        if(list.size()>0){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public boolean hasNickName(String nickName) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getNickName, nickName);
        List<User> list = list(queryWrapper);
        if(list.size()>0){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public boolean hasPhoneNumber(String phonenumber) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getPhonenumber, phonenumber);
        List<User> list = list(queryWrapper);
        if(list.size()>0){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public boolean hasEmail(String email) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getEmail, email);
        List<User> list = list(queryWrapper);
        if(list.size()>0){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public boolean addUser(User user) {
        //加密密码
        String encode = passwordEncoder.encode(user.getPassword());
        user.setPassword(encode);


        //添加用户
        boolean save = save(user);

        return save;
    }

    @Override
    public boolean updateUserStatus(Long userId, String status) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getId, userId);

        User user = new User();
        user.setStatus(status);
        boolean update = update(user, queryWrapper);

        return update;
    }

    private boolean userNameExist(String s){
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(User::getUserName, s);
        return  count(queryWrapper)>0;
    }
    private boolean nickNameExist(String s){
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(User::getNickName, s);
        return  count(queryWrapper)>0;
    }
    private boolean emailExist(String s){
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(User::getEmail, s);
        return  count(queryWrapper)>0;
    }
}
